<template>
    <div class="nut-coupon" :class="beanType" @click="isSwitch ? switchUse() : immediateUse()">
        <div class="nut-coupon-l">
            <template v-if="datas.couponType != 2 ">
                <div class="nut-coupon-fore1"><em>&yen;</em><span>{{datas.discount}}</span></div>
                <div class="nut-coupon-fore2">满{{datas.quota}}元可用</div>
            </template>
             <template v-else>
                <p class="nut-coupon-fore3">可抵扣运费</p>
                <p class="nut-coupon-fore4">除偏远山区、极速达</p>
            </template>
        </div>
        <div class="nut-coupon-r">
            <span class="nut-coupon-badge" v-if="isBadge"><slot></slot></span>
            <span class="nut-coupon-switch-use" v-if="isSwitch" :class="{'nut-coupon-switch-use-active': switchActive, 'nut-coupon-switch-use-unused': !datas.isUsed}"></span>
            <span class="nut-coupon-immediate-use" v-else>立即使用</span>
            <div class="nut-coupon-fore1">
                <em>{{['京券', '东券', '运费'][datas.couponType]}}</em><span>{{datas.couponLimitInfo}}</span>
            </div>
            <div class="nut-coupon-fore2">{{datas.beginTime}} - {{datas.endTime}}</div>
            <div class="nut-coupon-fore3"></div>
        </div>
    </div>
</template>
<script>
export default {
    name:'nut-coupon',
    props: {
        'datas':{
            type: Object,
            default: null
        },
        'isSwitch': {
            type: Boolean,
            default: false
        },
        'isBadge': {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            switchActive: false
        }
    },
    created(){

    },
    watch:{
        switchActive(newVal){
            this.switchActive = newVal;
        }
    },
    computed: {
        beanType: function() {
            return this.datas.isUsed ? ['nut-coupon-jbean', 'nut-coupon-dbean', 'nut-coupon-ybean'][this.datas.couponType] : 'nut-coupon-unbean';
        }
    },
    methods: {
        immediateUse() {
            if (!this.datas.isUsed) {
                return false;
            }
            this.$emit('immediate-use', this.datas);
        },
        switchUse() {
            if(!this.datas.isUsed) {
                return false;
            }
            this.switchActive = !this.switchActive;
            this.$emit('switch-use', this.datas, this.switchActive);
        }
    }
}
</script>
<style lang="scss">
$dBeanColor: #478dd4;
$jBeanColor: #ff7f7e;
$yBeanColor: #42cac0;
$unBeanColor: #c2c2c2;
.nut-coupon{
    height: 2rem;
    overflow: hidden;
    .nut-coupon-l{
        float: left;
        width: 2.5rem;
        height: 2rem;
        color: #fff;
        text-align: center;
        background-size: 100% 100%;
        .nut-coupon-fore1{
            height: 1.14rem;
            padding-top: 0.16rem;
            em{
                margin-right: 0.01rem;
                font-size: 0.4rem;
                font-style: normal;
            }
            span{
                font-size: 0.88rem;
                font-weight: bold;
            }
        }
        .nut-coupon-fore2{
            font-size: 0.24rem;
            line-height: 0.44rem;
        }
        .nut-coupon-fore3{
            height: 0.58rem;
            padding-top: 0.44rem;
            padding-bottom: 0.28rem;
            margin: 0;
            font-size: 0.38rem;
        }
        .nut-coupon-fore4{
            margin: 0;
            font-size: 0.22rem;
            line-height: 0.44rem;
        }
    }
    .nut-coupon-r{
        height: 1.78rem;
        margin-left: 2.5rem;
        padding: 0.2rem 0.22rem 0;
        background-color: #fff;
        overflow: hidden;
        border: 0.01rem solid #eaeaea;
        border-left: 0 none;
        border-radius: 0 3px 3px 0;
        position: relative;
        .nut-coupon-fore1{
            font-size: 0.24rem;
            color: #666;
            height: 0.6rem;
            em{
                display: inline-block;
                width: 0.72rem;
                padding: 0.01rem 0;
                border-radius: 3px;
                margin-right: 0.06rem;
                font-style: normal;
                text-align: center;
                font-size: 0.2rem;
                color: #fff;
            }
            em, span{
                vertical-align: middle;
            }
            span{
                line-height: 0.3rem;
                color: #686868;
            }
        }
        .nut-coupon-fore2{
            margin-top: 0.23rem;
            line-height: 0.24rem;
            color: #a5a5a5;
            font-size: 0.2rem;
        }
        .nut-coupon-fore3{
            margin-top: 0.21rem;
            height: 0.02rem;
            background: url() repeat-x left bottom;
        }
        .nut-coupon-immediate-use{
            position: absolute;
            right: 0.22rem;
            top: 0.92rem;
            height: 0.4rem;
            border-radius: 24px;
            width: 1.2rem;
            font-size: 0.22rem;
            line-height: 0.4rem;
            text-align: center;
            border-style: solid;
            border-width: 0.02rem;
        }
        .nut-coupon-switch-use{
            position: absolute;
            right: 0.2rem;
            top: 0.8rem;
            height: 0.36rem;
            width: 0.36rem;
            background: url() no-repeat;
            background-size: 100% 100%;
            &.nut-coupon-switch-use-active{
                background-image: url();
            }
            &.nut-coupon-switch-use-unused{
                background-image: url();
            }
        }
    }
    .nut-coupon-badge{
        position: absolute;
        right: 0;
        top: 0;
        height: 0.72rem;
        width: 0.94rem;
        background-size: 100% 100%;
    }
}
@mixin beanStatus($img,$BeanColor) {
    .nut-coupon-l{
        background-image: url('/asset/img/coupon/#{$img}');
    }
    .nut-coupon-r{
        .nut-coupon-fore1{
            em{
                background-color: $BeanColor;
            }
        }
        .nut-coupon-immediate-use{
            border-color: $BeanColor;
            color: $BeanColor;
        }
    }
}
.nut-coupon.nut-coupon-dbean{
    @include beanStatus('q-blue.png', $dBeanColor);
}
.nut-coupon.nut-coupon-jbean{
    @include beanStatus('q-red.png', $jBeanColor);
}
.nut-coupon.nut-coupon-ybean{
    @include beanStatus('q-green.png', $yBeanColor);
}
.nut-coupon.nut-coupon-unbean{
    @include beanStatus('q-grey.png', $unBeanColor);
}
</style>